# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：flask_model.py
#日期：2025-03-21
#备注：
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
from flask import Flask, make_response, request, abort, jsonify
import uuid

app = Flask(__name__)


@app.route('/.well-know/openid-configuration', methods=["GET"])
def openid_configuration():
    config = {
        "issuer": "https://apitest.cacho.com/openbanking",
        "claims_parameter_supported": True,
        "claims_supported": "open_banking_intent_id",
        "grant_types_supported": ["authorization_code", "client_credentials"],
        "response_types_supported": ["code", "code id_token"],
        "request_parameter_supported": True,
        "request_uri_parameter_supported": True,
        "scope_supported": ["openid", "accounts", "payments", "lg"],
        "token_endpoint_auth_methods_supported": ["tls_client_auth"],
        "token_endpoint": "https://apitest.cacho.com/openbanking/token",
        "authorization_endpoint": "https://apitest.cacho.com/openbanking/consent/sso/auth",
        "jwks_url": "https://apitest.cacho.com/openbanking/.well-know/jwks.json",
        "pushed_authorization_request_endpoint": "https://apitest.cacho.com/openbanking/par",
        "id_token_signing_alg_values_supported": ["PS256"],
        "request_object_signing_alg_values_supported": ["PS256"],
        "token_endpoint_auth_signing_alg_values_supported": ["PS256"],
        "mtls_endpoint_aliases": {
            "token_endpoint": "https://apitest.cacho.com/openbanking/token",
            "pushed_authorization_request_endpoint": "https://apitest.cacho.com/openbanking/par"
        },
        "tls_client_certificate_bound_access_tokens": True,
        "code_challenge_methods_supported": ["S256", "plain"],
        "subject_types_supported": ["public"]

    }
    return config


@app.route('/token', methods=["POST"])
def get_token():
    grant_type = request.form["grant_type"]
    if grant_type == "client_credentials":
        return uuid.uuid1()


@app.route('/account-access-consents', methods=["POST"])
def create_consent():
    data = request.json
    if data['Data']['Permissions']:
        data['Data']['ConsentId'] = uuid.uuid1()
    response = jsonify(data)
    response.status_code = 201
    response.headers['Location'] = f'/account-access-consents{data}'
    return response


@app.route('/account-access-consents/<ConsentId>', methods=["GET"])
def get_consent(ConsentId):
    if ConsentId is None:
        return jsonify({'ConsentId': uuid.uuid1()})
    else:
        return jsonify({'ConsentId': ConsentId})

if __name__ == '__main__':
    app.run(port=8080)
